package com.tuanzi.loan.business.repository.galaxycard.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductChannel.COMM_PRODUCT_CHANNEL;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductPrdid.COMM_PRODUCT_PRDID;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanGalaxyCardInfo.LOAN_GALAXY_CARD_INFO;

import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.galaxycard.GalaxyCardUserInfoRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.galaxycard.GalaxyCardUserInfoSearchRequest;
import com.tuanzi.loan.vo.response.galaxycard.GalaxyCardUserInfoSearchResponse;
import com.tuanzi.server.loan_base.util.DateUtils;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

/**
 * 银河黑卡用户信息 JOOQRepositoryInterface 实现
 * @author JinRuiyang 2018/1/8 19:01:54
 */
@Repository
public class GalaxyCardUserInfoRepositoryImpl extends JOOQBaseRepositoryPlus implements GalaxyCardUserInfoRepository {
    /**
     * GalaxyCardUserInfo查询
     *
     * @param searchRequest 请求信息
     * @param pageable      分页信息
     * @return 响应列表
     */
    @Override
    public Page<GalaxyCardUserInfoSearchResponse> list(GalaxyCardUserInfoSearchRequest searchRequest, Pageable pageable) {
        fields(LOAN_GALAXY_CARD_INFO.ID,
                LOAN_GALAXY_CARD_INFO.CREATE_TIME,
                LOAN_GALAXY_CARD_INFO.NO,
                LOAN_GALAXY_CARD_INFO.STATUS,
                LOAN_GALAXY_CARD_INFO.PHONE_NUMBER,
                LOAN_GALAXY_CARD_INFO.NAME.as("UserName"),
                LOAN_GALAXY_CARD_INFO.PLATFORM,
                LOAN_GALAXY_CARD_INFO.PROVINCE,
                LOAN_GALAXY_CARD_INFO.CITY,
                LOAN_GALAXY_CARD_INFO.AREA,
                LOAN_GALAXY_CARD_INFO.ADDRESS);
        fields(COMM_PRODUCT_PRDID.NAME);
        fields(COMM_PRODUCT_CHANNEL.CHANNEL_NAME);
        leftJoin(COMM_PRODUCT_PRDID, COMM_PRODUCT_PRDID.PRDID.cast(Long.class).eq(LOAN_GALAXY_CARD_INFO.PRODUCT_ID));
        leftJoin(COMM_PRODUCT_CHANNEL, COMM_PRODUCT_CHANNEL.CHANNEL_CODE.cast(Long.class).eq(LOAN_GALAXY_CARD_INFO.CHANNEL));
        eq(LOAN_GALAXY_CARD_INFO.PLATFORM,searchRequest.getPlatform());
        eq(LOAN_GALAXY_CARD_INFO.PRODUCT_ID,searchRequest.getProductId());
        eq(LOAN_GALAXY_CARD_INFO.CHANNEL,searchRequest.getChannelId());
        eq(LOAN_GALAXY_CARD_INFO.STATUS, searchRequest.getStatus());
        eq(LOAN_GALAXY_CARD_INFO.NO,searchRequest.getCardNo());
        eq(LOAN_GALAXY_CARD_INFO.PHONE_NUMBER,searchRequest.getPhoneNumber());
        eq(LOAN_GALAXY_CARD_INFO.NAME, searchRequest.getName());
        orderBy(LOAN_GALAXY_CARD_INFO.CREATE_TIME, Sort.Direction.DESC);
        return search(pageable, record -> {
            GalaxyCardUserInfoSearchResponse response = new GalaxyCardUserInfoSearchResponse();
            response.setId(record.get(LOAN_GALAXY_CARD_INFO.ID));
            response.setPk(record.get(LOAN_GALAXY_CARD_INFO.ID));
            response.setActiveTime(DateUtils.formatDateToYyyyMMddHHmmss(record.get(LOAN_GALAXY_CARD_INFO.CREATE_TIME)));
            response.setCardNo(record.get(LOAN_GALAXY_CARD_INFO.NO));
            response.setName(PhoneNumNideUtil.hideName(record.get("UserName",String.class)));
            response.setAddress(PhoneNumNideUtil.hideAddress(record.get(LOAN_GALAXY_CARD_INFO.ADDRESS)));
            response.setPhoneNumber(PhoneNumNideUtil.hideNum(record.get(LOAN_GALAXY_CARD_INFO.PHONE_NUMBER)));
            
            response.setPlatform(record.get(LOAN_GALAXY_CARD_INFO.PLATFORM));
            response.setStatus(record.get(LOAN_GALAXY_CARD_INFO.STATUS));
            response.setProduct(record.get(COMM_PRODUCT_PRDID.NAME));
            response.setChannel(record.get(COMM_PRODUCT_CHANNEL.CHANNEL_NAME));
            response.setProvince(record.get(LOAN_GALAXY_CARD_INFO.PROVINCE));
            response.setCity(record.get(LOAN_GALAXY_CARD_INFO.CITY));
            response.setArea(record.get(LOAN_GALAXY_CARD_INFO.AREA));
            return response;
        });
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_GALAXY_CARD_INFO;
    }
}
